import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题


# 创建参数网格
theta1 = np.linspace(-10, 10, 100)
theta2 = np.linspace(-10, 10, 100)
T1, T2 = np.meshgrid(theta1, theta2)

# 计算损失函数
J = 0.5*(T1**2 + 2*T1*T2 + 3*T2**2)  # 简化损失函数
J_reg = J + 0.5*(T1**2 + T2**2)       # 加入L2正则化

# 绘制等高线
plt.figure(figsize=(12, 5))
plt.subplot(121)
plt.contour(T1, T2, J, levels=20)
plt.title("原始损失函数")
plt.xlabel(r"$\theta_1$")
plt.ylabel(r"$\theta_2$")

plt.subplot(122)
plt.contour(T1, T2, J_reg, levels=20)
plt.title("加入L2正则化的损失函数")
plt.xlabel(r"$\theta_1$")
plt.show()


iterations = range(1, 100)
liblinear_loss = [1/(0.1*i) for i in iterations]
sag_loss = [1/(0.3*i) for i in iterations]
saga_loss = [1/(0.4*i) for i in iterations]
newton_loss = [0.9**i for i in iterations]

plt.figure(figsize=(10, 6))
plt.plot(iterations, liblinear_loss, label='liblinear (线性收敛)')
plt.plot(iterations, sag_loss, label='SAG (线性收敛)')
plt.plot(iterations, saga_loss, label='SAGA (线性收敛)')
plt.plot(iterations, newton_loss, label='newton-cg (二次收敛)')
plt.yscale('log')
plt.xlabel('迭代次数')
plt.ylabel('损失值 (对数尺度)')
plt.title('优化算法收敛速度比较')
plt.legend()
plt.grid(True)
plt.show()